/*
 * Copyright (c) 2018. Lorem ipsum dolor sit amet, consectetur adipiscing elit.
 * Morbi non lorem porttitor neque feugiat blandit. Ut vitae ipsum eget quam lacinia accumsan.
 * Etiam sed turpis ac ipsum condimentum fringilla. Maecenas magna.
 * Proin dapibus sapien vel ante. Aliquam erat volutpat. Pellentesque sagittis ligula eget metus.
 * Vestibulum commodo. Ut rhoncus gravida arcu.
 */

package com.j4dream.property.dao;

import com.google.common.collect.ImmutableList;
import com.j4dream.property.BaseDaoTestCase;
import com.j4dream.property.model.PermissionDto;
import com.j4dream.property.model.RoleDto;
import com.j4dream.property.model.UserDto;
import org.junit.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.test.annotation.Rollback;

import java.util.List;

import static org.junit.Assert.*;


public class UserDaoTestCase extends BaseDaoTestCase {

    @Autowired
    private PermissionDao permissionDao;

    @Autowired
    private UserDao userDao;

    @Autowired
    private RoleDao roleDao;

    @Test
    public void insertPost() {
    }

    @Test
    public void insertUser() {
        UserDto userDto = new UserDto();
        userDto.setEmail("admin@j4dream.com");
        userDto.setName("j4dream");
        userDto.setFirstName("Dream");
        userDto.setLastName("Long");
        userDto.setPassword("password");
        userDao.insertUser(userDto);

        List<UserDto> savedUserList = userDao.getUserList();
        UserDto savedUserDto = savedUserList.get(0);
        assertEquals(0, savedUserDto.getRoles().size());
        assertEquals("admin@j4dream.com", savedUserDto.getEmail());
        assertEquals("j4dream", savedUserDto.getName());
        assertEquals("Dream", savedUserDto.getFirstName());
        assertEquals("Long", savedUserDto.getLastName());
        assertEquals("password", savedUserDto.getPassword());

    }

    @Test
    public void grantRoles() {
        PermissionDto createPerm = new PermissionDto("TEST_PERM_CREATE", "test create perm");
        PermissionDto updatePerm = new PermissionDto("TEST_PERM_UPDATE", "test update perm");
        permissionDao.insertPermission(createPerm);
        permissionDao.insertPermission(updatePerm);

        RoleDto roleDto = new RoleDto();
        roleDto.setName("ADMIN");
        roleDao.insertRole(roleDto);
        roleDao.grantPermission(roleDto.getId(), ImmutableList.of(createPerm.getId(), updatePerm.getId()));

        UserDto userDto = new UserDto();
        userDto.setEmail("admin@j4dream.com");
        userDto.setName("j4dream");
        userDto.setFirstName("Dream");
        userDto.setLastName("Long");
        userDto.setPassword("password");
        userDao.insertUser(userDto);
        userDao.grantRoles(userDto.getId(), ImmutableList.of(roleDto.getId()));


        List<UserDto> userDtoList = userDao.getUserList();
        UserDto savedUser = userDtoList.get(0);

        assertEquals(1, savedUser.getRoles().size());
        RoleDto savedRole = savedUser.getRoles().get(0);

        assertEquals(2, savedRole.getPermissions().size());
        List<PermissionDto> savedPermissions = savedRole.getPermissions();

        assertEquals("admin@j4dream.com", savedUser.getEmail());
        assertEquals("j4dream", savedUser.getName());
        assertEquals("Dream", savedUser.getFirstName());
        assertEquals("Long", savedUser.getLastName());
        assertEquals("password", savedUser.getPassword());

        assertEquals("ADMIN", savedRole.getName());

        assertEquals("TEST_PERM_CREATE", savedPermissions.get(0).getName());
        assertEquals("TEST_PERM_UPDATE", savedPermissions.get(1).getName());
    }

    @Test
    public void findUserByEmail() {
    }

    @Test
    public void findOneByEmail() {
        PermissionDto permissionDto = new PermissionDto("TEST_PERM", "For test.");
        permissionDao.insertPermission(permissionDto);

        RoleDto roleDto = new RoleDto("ADMIN");
        roleDao.insertRole(roleDto);
        roleDao.grantPermission(roleDto.getId(), ImmutableList.of(permissionDto.getId()));

        UserDto userDto = new UserDto();
        userDto.setEmail("test@gmail.com");
        userDto.setName("test");
        userDto.setFirstName("dream");
        userDto.setLastName("long");
        userDao.insertUser(userDto);

        userDao.grantRole(userDto.getId(), ImmutableList.of(roleDto.getId()));
        //UserDto findUserDto = userDao.findOneByEmail("test@gmail.com");

    }

    @Test
    public void findUserByEmailAndPassword() {
    }

    @Test
    public void existEmail() {
    }

    @Test
    public void getUserList() {
    }

    @Test
    @Rollback(false)
    public void delUser() {
        PermissionDto permissionDto = new PermissionDto("TEST_PERM", "For test.");
        permissionDao.insertPermission(permissionDto);

        RoleDto roleDto = new RoleDto("ROLE_DEL");
        roleDao.insertRole(roleDto);
        roleDao.grantPermission(roleDto.getId(), ImmutableList.of(permissionDto.getId()));

        UserDto userDto = new UserDto();
        userDto.setEmail("test@gmail.com");
        userDto.setName("test");
        userDto.setFirstName("dream");
        userDto.setLastName("long");
        userDao.insertUser(userDto);
        int userId = userDto.getId();
        assertEquals(1, userDao.getUserList().size());

        userDao.deleteUserById(userId);

        assertEquals(0, userDao.getUserList().size());

    }
}